package com.sskj.market.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.NumberUtil;
import com.sskj.market.R;
import com.sskj.market.R2;
import com.sskj.market.bean.GoodCategoryBean;
import com.sskj.market.http.HttpConfig;
import com.sskj.market.presenter.GoodCategoryPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 商城下的分类
 *
 * @author Hey
 * Create at  2019/07/31
 */
@Route(path = RConfig.MARKET_GOOD_CATEGORY)
public class GoodCategoryActivity extends BaseActivity<GoodCategoryPresenter> {
    @Autowired
    String title;
    @Autowired
    int type;
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvReturn)
    TextView tvReturn;
    @BindView(R2.id.ivBack)
    LinearLayout ivBack;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R2.id.tvRight)
    TextView tvRight;
    private BaseAdapter<GoodCategoryBean.DataBean> adapter;
    private int page = 1;

    @Override
    public int getLayoutId() {
        return R.layout.market_activity_good_category;
    }

    @Override
    public GoodCategoryPresenter getPresenter() {
        return new GoodCategoryPresenter();
    }

    @SuppressLint("NewApi")
    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        tvTitle.setText(title);
        mPresenter.getGoodCategoryData(page, type, true);

        ClickUtil.click(300, tvRight, () -> {
            ARouter.getInstance().build(RConfig.MARKET_ACTIVITY_SEARCH).withInt("category_id", type).navigation();
        });

        smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getGoodCategoryData(page, type, false);
        });

        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getGoodCategoryData(page, type, false);
        });
    }

    @Override
    public void initData() {
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));


        adapter = new BaseAdapter<GoodCategoryBean.DataBean>(R.layout.activity_shopping_item, null, recyclerView, true) {
            @SuppressLint("CheckResult")
            @Override
            public void bind(ViewHolder holder, GoodCategoryBean.DataBean item) {

                holder.setText(R.id.tvTitleShopItem, item.getName())
                        .setText(R.id.tvMoneyShopItem, NumberUtil.keep2(item.getPrice()) + "LAC")
                        .setText(R.id.tvNumberShopItem, item.getPurchase_number() + App.INSTANCE.getString(R.string.market_shoppingActivity2));
                ImageView ivShopItem = holder.getView(R.id.ivShopItem);

                if (item.getPics().size() != 0) {
                    RequestOptions requestOptions = new RequestOptions();
                    requestOptions.placeholder(R.mipmap.market_loading_img);
                    Glide.with(GoodCategoryActivity.this).load(HttpConfig.BASE_IMG_URL + item.getPics().get(0)).apply(requestOptions).into(ivShopItem);
                }

                LinearLayout linearLayout = holder.getView(R.id.llItem);
                ClickUtil.click(300, linearLayout, () -> {
                    ARouter.getInstance().build(RConfig.MARKET_ACTIVITY_COMMODITY_DETAILS).withInt("product_id", item.getId()).navigation();
                });
            }
        };
    }

    public void getGoodCategoryDataSuccess(GoodCategoryBean data) {
        if (data.getData() == null) {
            return;
        }

        if (page==1){
            adapter.setNewData(data.getData());
        }else {
            adapter.addData(data.getData());
        }

    }

    public void getGoodRefreshSuccess() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishRefresh();
            smartRefreshLayout.finishLoadMore();
        }
    }
}
